<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-sub-content">
  <div class="gv-forms" layout="column">
    <div class="gv-forms-header" layout="row" layout-align="start end" style="display: flex; align-items: center; margin-top: 16px">
      <gravitee-identity-picture
        image-border-radius="true"
        image-id="$ctrl.selectedUser.id"
        image-name="$ctrl.selectedUser.displayName"
        image-url="$ctrl.getUserPicture()"
        image-width="50"
      ></gravitee-identity-picture>

      <h1 class="user">{{$ctrl.selectedUser.displayName}}</h1>
      <a ng-click="$ctrl.backToUsers()" style="font-size: 12px; font-weight: normal">Back to users</a>
    </div>

    <div class="gv-form">
      <h2></h2>
      <div class="gv-form-content" layout="column">
        <md-table-container layout="row" layout-align="center center">
          <table md-table>
            <tbody md-body>
              <tr md-row style="height: 30px">
                <td md-cell width="15%"><span style="font-weight: bold">Email</span></td>
                <td md-cell width="35%">{{$ctrl.selectedUser.email}}</td>
                <td md-cell width="15%"><span style="font-weight: bold">Source</span></td>
                <td md-cell width="35%">{{$ctrl.selectedUser.source}}</td>
              </tr>
              <tr md-row ng-if="$ctrl.selectedUser.customFields" ng-repeat="item in $ctrl.getUserCustomFieldsPairs()">
                <td md-cell width="15%"><span style="font-weight: bold">{{$ctrl.getCustomFieldLabel(item.key1)}}</span></td>
                <td md-cell width="35%">{{item.value1}}</td>
                <td md-cell width="15%"><span style="font-weight: bold">{{$ctrl.getCustomFieldLabel(item.key2)}}</span></td>
                <td md-cell width="35%">{{item.value2}}</td>
              </tr>
              <tr md-row style="background-color: white; height: 30px">
                <td md-cell><span style="font-weight: bold">Created at</span></td>
                <td md-cell>{{$ctrl.selectedUser.created_at | date:'MMM d, y h:mm:ss a'}}</td>
                <td md-cell nowrap><span style="font-weight: bold">Last connection at</span></td>
                <td md-cell>{{$ctrl.selectedUser.lastConnectionAt | date:'MMM d, y h:mm:ss a'}}</td>
              </tr>
              <tr md-row style="background-color: white; height: 30px">
                <td md-cell nowrap><span style="font-weight: bold">Organization Role</span></td>
                <td md-cell>
                  <md-select
                    class="inline-multiple-select"
                    ng-model="selectedOrganizationRole"
                    ng-disabled="$ctrl.selectedUser.status !== 'ACTIVE'"
                    aria-label="organization Role"
                    md-on-open="selectOpened = true"
                    md-on-close="selectOpened = false"
                    ng-change="$ctrl.updateOrganizationsRole(selectOpened, selectedOrganizationRole)"
                    multiple
                  >
                    <md-option ng-value="role.id" ng-repeat="role in $ctrl.organizationRoles">{{role.name}}</md-option>
                  </md-select>
                </td>
                <td md-cell><span style="font-weight: bold">Status</span></td>
                <td md-cell>{{$ctrl.selectedUser.status}}</td>
              </tr>
            </tbody>
          </table>
        </md-table-container>
        <div layout="row" layout-align="center center" ng-if="$ctrl.selectedUser.status === 'ACTIVE'">
          <md-button
            type="button"
            class="md-raised md-warn"
            permission
            permission-only="'organization-user-u'"
            aria-label="User reset password"
            style="fill: white"
            ng-if="$ctrl.selectedUser.source === 'gravitee'"
            ng-click="$ctrl.resetPasswordDialog()"
          >
            <ng-md-icon icon="autorenew"></ng-md-icon>
            Reset user password
          </md-button>
        </div>
        <div class="md-actions gravitee-api-save-button" layout="row" ng-if="$ctrl.selectedUser.status === 'PENDING'">
          <div permission permission-only="'organization-user-u'">
            <md-button class="md-raised" aria-label="Accept user registration" ng-click="$ctrl.accept($event)">
              <ng-md-icon icon="done" style="fill: green"></ng-md-icon>
              Accept
            </md-button>
            <md-button class="md-raised md-warn" aria-label="User reset password" ng-click="$ctrl.reject($event)">
              <ng-md-icon icon="block" style="fill: white"></ng-md-icon>
              Reject
            </md-button>
          </div>
        </div>
      </div>

      <div class="gv-form">
        <h2></h2>
        <div class="gv-form-content" layout="column">
          <md-tabs md-dynamic-height>
            <md-tab md-on-select="currentTab='environments'; $ctrl.loadUserEnvironments();">
              <md-tab-label>Environments</md-tab-label>
              <md-tab-body>
                <div style="margin: 16px 0 8px">Here is the list of all environment this user can access.</div>
                <md-table-container>
                  <table class="gv-table-dense" md-table>
                    <thead md-head>
                      <tr md-row>
                        <th md-column>Name</th>
                        <th md-column>Description</th>
                        <th md-column>Roles</th>
                      </tr>
                    </thead>
                    <tbody md-body>
                      <tr ng-if="userEnvironments.length === 0" style="height: 30px">
                        <td colspan="2" md-cell style="text-align: center">None.</td>
                      </tr>
                      <tr md-row ng-repeat="environment in $ctrl.environments">
                        <td md-cell>{{environment.name}}</td>
                        <td md-cell>{{environment.description}}</td>
                        <td md-cell>
                          <md-select
                            aria-label="environment Role"
                            class="inline-multiple-select"
                            md-on-close="selectOpened = false"
                            md-on-open="selectOpened = true"
                            multiple
                            ng-change="$ctrl.updateEnvironmentsRole(selectOpened, environment.id, selectedEnvironmentRole[environment.id])"
                            ng-disabled="$ctrl.selectedUser.status !== 'ACTIVE'"
                            ng-model="selectedEnvironmentRole[environment.id]"
                          >
                            <md-option ng-repeat="role in $ctrl.environmentRoles" ng-value="role.id">{{role.name}}</md-option>
                          </md-select>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </md-table-container>
              </md-tab-body>
            </md-tab>
            <md-tab md-on-select="currentTab='groups'">
              <md-tab-label>Groups</md-tab-label>
              <md-tab-body>
                <div style="margin: 16px 0 8px">You can manage all groups of this user.</div>
                <md-table-container>
                  <table class="gv-table-dense" md-table>
                    <thead md-head>
                      <tr md-row>
                        <th md-column>Name</th>
                        <th md-column>Group Admin</th>
                        <th md-column>API Role</th>
                        <th md-column>Application Role</th>
                        <th md-column></th>
                      </tr>
                    </thead>
                    <tbody md-body>
                      <tr ng-if="$ctrl.groups.length === 0" style="height: 30px">
                        <td colspan="4" md-cell style="text-align: center">None.</td>
                      </tr>
                      <tr md-row ng-repeat="group in $ctrl.groups">
                        <td md-cell>{{group.name}}</td>
                        <td md-cell>
                          <md-checkbox
                            aria-label="Administrator of this group"
                            ng-change="$ctrl.updateGroupRole(group)"
                            ng-false-value="''"
                            ng-model="group.roles['GROUP']"
                            ng-true-value="'ADMIN'"
                          >
                          </md-checkbox>
                        </td>
                        <td md-cell>
                          <md-select
                            aria-label="membership-api-role"
                            ng-change="$ctrl.updateGroupRole(group)"
                            ng-model="group.roles['API']"
                          >
                            <md-option ng-disabled="role.system" ng-repeat="role in $ctrl.apiRoles" ng-value="role.name"
                              >{{role.name}}</md-option
                            >
                          </md-select>
                        </td>
                        <td md-cell>
                          <md-select
                            aria-label="membership-application-role"
                            ng-change="$ctrl.updateGroupRole(group)"
                            ng-model="group.roles['APPLICATION']"
                          >
                            <md-option ng-disabled="role.system" ng-repeat="role in $ctrl.applicationRoles" ng-value="role.name"
                              >{{role.name}}</md-option
                            >
                          </md-select>
                        </td>
                        <td md-cell ng-click="$event.stopPropagation()">
                          <div layout="row" layout-align="end center">
                            <span permission permission-only="['organization-user-d']">
                              <md-tooltip md-direction="top">delete</md-tooltip>
                              <ng-md-icon aria-label="delete-user" icon="delete" ng-click="$ctrl.remove($event, group)"></ng-md-icon>
                            </span>
                          </div>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </md-table-container>
              </md-tab-body>
            </md-tab>
            <md-tab md-on-select="currentTab='apis'; $ctrl.loadUserApis();">
              <md-tab-label>APIs</md-tab-label>
              <md-tab-body>
                <div style="margin: 16px 0 8px">Here is the list of all APIs of this user.</div>
                <md-table-container>
                  <table class="gv-table-dense" md-table>
                    <thead md-head>
                      <tr md-row>
                        <th md-column>Name</th>
                        <th md-column>Version</th>
                        <th md-column>Visibility</th>
                      </tr>
                    </thead>
                    <tbody md-body>
                      <tr ng-if="userApis.length === 0" style="height: 30px">
                        <td colspan="3" md-cell style="text-align: center">None.</td>
                      </tr>
                      <tr md-row ng-repeat="api in userApis">
                        <td md-cell>{{api.name}}</td>
                        <td md-cell>{{api.version}}</td>
                        <td md-cell>
                          <ng-md-icon icon="{{api.visibility==='PUBLIC' ? 'public' : 'lock'}}" size="20" style="fill: #cdcdcd">
                            <md-tooltip>{{api.visibility}}</md-tooltip>
                          </ng-md-icon>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </md-table-container>
              </md-tab-body>
            </md-tab>
            <md-tab md-on-select="currentTab='applications'; $ctrl.loadUserApplications();">
              <md-tab-label>Applications</md-tab-label>
              <md-tab-body>
                <div style="margin: 16px 0 8px">Here is the list of all applications of this user.</div>
                <md-table-container>
                  <table class="gv-table-dense" md-table>
                    <thead md-head>
                      <tr md-row>
                        <th md-column>Name</th>
                        <th md-column>Type</th>
                      </tr>
                    </thead>
                    <tbody md-body>
                      <tr ng-if="userApplications.length === 0" style="height: 30px">
                        <td colspan="2" md-cell style="text-align: center">None.</td>
                      </tr>
                      <tr md-row ng-repeat="application in userApplications">
                        <td md-cell>{{application.name}}</td>
                        <td md-cell>{{application.type}}</td>
                      </tr>
                    </tbody>
                  </table>
                </md-table-container>
              </md-tab-body>
            </md-tab>
          </md-tabs>
        </div>
      </div>
    </div>

    <md-button
      aria-label="Add"
      class="md-fab md-fab-bottom-right md-fab-scrollable"
      ng-class="{'gv-help-displayed': $ctrl.$rootScope.helpDisplayed}"
      ng-click="$ctrl.addGroupDialog()"
      ng-if="$ctrl.selectedUser.status === 'ACTIVE'"
      permission
      permission-only="['organization-user-c']"
    >
      <ng-md-icon icon="add"></ng-md-icon>
    </md-button>
  </div>
</div>
